#include "password.h"
Visitor *p;//申请堆内存，存放登录信息
Visitor *v;//活跃指针
int exit_flag;//系统即将退出的标志
bool quit_flag;
//主界面
void Home(void)
{
	int num = 0;
	while(num!=50)
	{
		CLC;
		printf("----欢迎来到学生管理系统----\n");	
		printf("1->【新用户注册】\n");
		printf("2->【用户登录】\n");
		printf("3->【管理员模式】\n");
		printf("4->【退出系统】\n");
		num=getch();
		switch(num)
		{
			case 49:
			if(Getgroupnumber('a')==0)
			Register(p,'a');
			else Register(p,'s');
			break;	
			case 50:v=Signin();return; break;
			case 51:Administratorsmod(); break;
			case 52:exit_flag=1; return; break;
			default:break;
		}
	}
	return;
}
//新用户注册
void Register(Visitor *w,const char level)
{
	char buf[5];
	if(Getgroupnumber('o')<MAX_NUMBER)
	{
		while(Translate(w->level)!=NULL&&Getgroupnumber('o')!=0) w++;
	}
	else
	{
		printf("系统注册成员已达上限!\n");
		return;
	}
	CLC;
	if(Getgroupnumber('a')==0)
	printf("系统未检测到校长账号存在,自动为您开启快速注册\n");
	printf("快速注册:请输入%s姓名:",Translate(level));
	while(scanf("%16s",w->name),strlen(w->name)>16)
	{
		CLC;
		printf("名字长度不要超过16字符(一个汉字算2字符),超过则系统不会读取!\n");	
		printf("快速注册:请输入%s姓名:",Translate(level));
	}
	stdin -> _IO_read_ptr = stdin -> _IO_read_end;//清除缓冲区
	while(Samename(w->name))
	{
		printf("由于系统中有重名用户,自动生成乱码后缀\n");
		srand((unsigned)(time(NULL)));
		sprintf(buf,"%d",(rand()%10000));
		strcat(w->name,buf);
		printf("修改姓名为%s\n",w->name);
	}
	printf("快速注册:请选择%s性别(m:男,w:女):",Translate(level));
	scanf(" %c",&w->sex);
	while(w->sex!='w'&&w->sex!='m')
	{
		CLC;
		printf("性别输入错误!请重试\n");	
		printf("快速注册:请选择%s性别(m:男,w:女):",Translate(level));
		scanf(" %c",&w->sex);
	}
			srand((unsigned)(time(NULL)));
			w->id=rand()%MAX_NUMBER;
		Visitor *dest=&p[0];
	while(Translate(dest->level)!=NULL)
	{
		if(w->id==dest->id)
		{
			srand((unsigned)(time(NULL)));
			w->id=rand()%MAX_NUMBER;
			dest=&p[0];
		}
		dest++;
	}//为了保证生成的学工号不重复
	w->enter_error=false;
	w->issigned=false;
	strcpy(w->password,RESET_PASSWORD);
	w->level=level;
	w->grade[0] = 0;
	w->grade[1] = 0;
	w->grade[2] = 0;
	printf("系统自动注册成功！该%s学工号是%d,密码是%s,等级是%s,按任意键后跳转界面...\n",Translate(w->level),w->id,w->password,Translate(w->level));
	Writesigninmsg("signin.txt",w);
	Anykey();
	return;
}
void Administratorsmod(void)
{
	char spassword[13];
	int i=0;
	printf("请输入超级管理员密码:");
	while((spassword[i]=getch())!='\n')
	{
		if(spassword[i]=='\b')
		{
			printf("\b \b");	
			i--;
		}	
		else
		{
			printf("*");
			i++;
		}
	}
	spassword[i]='\0';
	if(strcmp(SUPERPASSWORD,spassword)==0)
	{
		Supercontrol();	
	}
	else
	{
		printf("密码输入错误!\n");	
		return;
	}
}
void Supercontrol(void)
{
	printf("校长账号:%d 校长密码:%s\n",p->id,p->password);
	Anykey();
	return;
}

//登录系统
Visitor* Signin()
{
	if(Getgroupnumber('o')==0)
	Register(p,'a');
	int sid = 0,thisid;
	char password[16] = {};
	char truepaswrd[16] = {};
	int r_time=3;
	int flag=false;
	while(r_time > 0 && (strcmp(password,truepaswrd) != 0 || sid != thisid)){
	CLC;
	printf("----欢迎来到学生管理系统----\n");
	if(r_time != 3) printf("密码输入错误！你还有%d次机会\n",r_time);
	else if(flag)printf("没有该账号或该账号已注销！\n");
	printf("请输入学/工号:");
	scanf("%d",&sid);
	if(Findid(sid))
	{
		if(Findmember(sid)->enter_error==true)
		{
			printf("该账号已经锁定!\n");
			Anykey();
			return NULL;
		}
		thisid = sid;
		Visitor *dest = Findmember(sid);
		strcpy(truepaswrd,dest->password);
	}
	else r_time=4;
		printf("请输入密码:");
		scanf("%s",password);
		r_time--;
		flag=true;
	printf("----------------------------\n");
	}
	Visitor *dest = Findmember(sid);
	if(r_time == 0){
		Findmember(sid)->enter_error = true;
		printf("你已经输错3次密码,账号锁定!请联系%s解锁\n",Translate(Higherlevel(Findmember(sid)->level)));
		Modifysigninmsg("signin.txt",dest,getCurrentIndex(sid));
		Anykey();
		return NULL;		
	}
	return dest;
}
void Rewrite(const int id)
{
	Visitor *dest = Findmember(id);
	char before[16] = {};
	char newpassword[16] = {};
	char newpassword2[16] = {};
	if(dest->issigned != true)
	printf("因为您是第一次登录，您需要更改密码\n");
		printf("请输入旧密码：");
	while(scanf("%s",before),strcmp(before,dest->password)!=0)
	{
		CLC;
		printf("旧密码输入错误，请重新输入！\n");
		printf("请输入旧密码：");
	}
			printf("请输入新密码：");
			while(scanf("%s",newpassword),!Islegal(newpassword))
	{
		CLC;
		printf("新密码非法!请输入字母(检查大小写),数字或下划线的组合作为你的密码且密码长度不要超过16!\n");
		printf("请输入新密码：");
					
	}
			printf("请再输入一次新密码:");
			while(scanf("%s",newpassword2),strcmp(newpassword,newpassword2) != 0)
			{
				CLC;
				printf("新密码输入错误,请重新输入！\n");
				printf("请再输入一次新密码:");
			}
			printf("密码修改成功！\n");
			strcpy(dest->password,newpassword2);
			dest->issigned = true;
			Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
			return;
}
void Reset(const int id,const char level)
{
	char password[16]={};
	strcpy(password,RESET_PASSWORD);
	if(false == Findid(id))
	{
		printf("系统未检测到该学工号存在!\n");	
		Anykey();
		return;
	}
	Visitor *dest = Findmember(id);
	if(Translate(dest->level) == NULL)
	{
		printf("没有该账号!\n");
		Anykey();
		return;
	}
	if(Levelcmp(level,dest->level)==true)
	{
		printf("密码重置成功！初始密码是:%s\n",password);
		strcpy(dest->password,password);
		dest->enter_error=false;
		Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
		Anykey();
		return;
	}
	else
	{
		printf("您无权限进行此操作！\n");	
		Anykey();
		return;
	}
}
void DisplayTeacherList(void)
{
	CLC;
	int id;
	char cmd;
	char name[20];
	printf("----输入数字选择对应功能----\n");
	printf("1:-------->添加学生<--------\n");
	printf("2:-------->删除学生<--------\n");
	printf("3:-------->查找学生<--------\n");
	printf("4:------>修改学生信息<------\n");
	printf("5:------>录入学生成绩<------\n");
	printf("6:------>重置学生密码<------\n");
	printf("7:-->显示所有在校学生信息<--\n");
	printf("8:-->显示所有退出学生信息<--\n");
	printf("回车键:------>退出登录<-----\n");
	switch(getch())
	{
		case 49:
			printf("请选择导入方式(s:单个导入,b:批量导入):");
			scanf("%c",&cmd);
			if(cmd=='s') Register(p,Lowerlevel(v->level));//单个导入学生
			else if(cmd=='b') Batchimport("stuinfo.txt",v->level);	
			else
			{
				printf("输入命令有误!\n");
				Anykey();
			}
			break;
		case 50:
			printf("请输入你想删除的%s学工号:",Translate(Lowerlevel(v->level)));
			scanf("%d",&id);
			Delete(id,v->level);
			break;
		case 51:
			printf("请选择你要查找学生的方式(n:按姓名查找，i:按学工号查找):\n");
			scanf("%c",&cmd);
			if(cmd=='i')
			{
				printf("请输入要查找的%s学工号:",Translate(Lowerlevel(v->level)));
				scanf("%d",&id);
				Displayonemsg(id,v->level);
			}
			else if(cmd=='n')
			{
				printf("请输入要查找的%s姓名:",Translate(Lowerlevel(v->level)));
				scanf("%s",name);
				Samefind(name);
			}
			else
			{
				printf("输入命令有误!\n");
				Anykey();
			}
			break;
		case 52:
			printf("请输入你想修改的%s的id和修改方向(g:修改成绩,i:修改信息,用空格隔开):\n",Translate(Lowerlevel(v->level)));
			scanf("%d %c",&id,&cmd);
			Modify(id,cmd,v->level);
			break;
		case 53:
			printf("请选择成绩导入方式(s:单个导入,b:批量导入):");
			scanf("%c",&cmd);
			if(cmd=='s')
			{
				printf("请输入你想录入成绩的学生学工号:");
				scanf("%d",&id);
				Insertgrade(id,v->level);
			}
			else if(cmd=='b')
			{
				Gradebatch("stu_grade.txt",v->level);	
			}
			else
			{
				printf("输入命令有误!\n");
				Anykey();
			}
		break;
		case 54:
			printf("请输入你想重置的%s学工号:",Translate(Lowerlevel(v->level)));
			scanf("%d",&id);
			Reset(id,v->level);
		break;
		case 55:
		Displayallmsg('s',v->level);//显示所有在校学生信息
		break;
		case 56:
		Displayallmsg('d',v->level);
		break;
		case 10:quit_flag=true;break;
		default:break;
	}
	return;
}
void DisplayAdminList(void)
{
	CLC;
	char cmd;
	int id;
	printf("----输入数字选择对应功能----\n");
	printf("1:-------->重置密码<--------\n");
	printf("2:------>重置教师密码<------\n");
	printf("3:-------->添加教师<--------\n");
	printf("4:-------->删除教师<--------\n");
	printf("5:---->显示所有在职教师<----\n");
	printf("6:---->显示所有离职教师<----\n");
	printf("回车键:------>退出登录<-----\n");
	switch(getch())
	{
		case 49:Reset(v->id,v->level);//重置密码
				break;
		case 50://重置教师密码
				printf("请输入你想重置的%s学工号:",Translate(Lowerlevel(v->level)));
				scanf("%d",&id);
				Reset(id,v->level);
				break;
		case 51://添加教师
			printf("请键入命令选择导入方式(s:单个导入,b:批量导入):");
			scanf("%c",&cmd);
			if(cmd=='s') Register(p,Lowerlevel(p->level));
			else if(cmd=='b') Batchimport("tchinfo.txt",p->level);	
			else
			{
				printf("命令输入错误!\n");
				Anykey();
			}
			break;
		case 52://删除教师
			printf("请输入你想删除的%s学工号:",Translate(Lowerlevel(v->level)));
			scanf("%d",&id);
			Delete(id,v->level);
			break;
		case 53:
		Displayallmsg('t',v->level);//显示所有在职教师
		break;
		case 54:
		Displayallmsg('q',v->level);//显示所有离职教师
		break;
		case 10:quit_flag=true;break;
		default:break;
	}
	return;
}
void DisplayStudentList(void)
{
	CLC;
	printf("----输入数字选择对应功能----\n");
	printf("1:-------->查询成绩<--------\n");
	printf("2:-------->修改密码<--------\n");
	printf("3:------>察看个人信息<------\n");
	printf("回车键:------>退出登录<-----\n");
	switch(getch())
	{
		case 49:Displaygrade(v->id,v->level);//查询成绩
		break;
		case 50:Rewrite(v->id);//改写密码
		break;
		case 51:Displayonemsg(v->id,v->level);
		break;
		case 10:quit_flag=true; break;
		default:break;
	}
}


void Displaychoicelist(const char level)
{
	CLC;
	printf("----欢迎进入学生管理系统----\n");
	switch(level)
	{
		case 'a':DisplayAdminList(); break;
		case 't':DisplayTeacherList(); break;
		case 's':DisplayStudentList(); break;
		default:printf("您不是本校成员，无权访问！\n");Anykey();quit_flag=true; break;
		return;
	}
}


